package com.lwt.spock

import com.lwt.spock.dao.UserDao
import com.lwt.spock.pojo.dto.UserDTO
import com.lwt.spock.service.UserService
import spock.lang.Specification
import spock.lang.Unroll

class UserService2Test extends Specification{
    def userDao = Mock(UserDao)
    def userService = new UserService(userDao: userDao)
    def "test getUserService"(){
        given: "设置请求参数"
        def user1 = new UserDTO(id:1,name: "张三",province: "上海")
        def user2 = new UserDTO(id:2,name: "李四",province: "江苏")
        and:"mock掉接口返回的用户信息"
        userDao.getUserInfoList() >> [user1,user2]

        when: "调用获取用户信息方法"
        def response  = userService.getUserById(1)

        then : "验证返回结果是否符合预期值"
        with(response){
            name == "张三"
            abbreviation == "沪"
            postCode == 200000
        }
    }
    // 注解,可以把每一次调用作为一个单独的测试用例运行
    @Unroll
    def "input 用户id:#id, 返回的邮编:#postCodeResult, 返回的省份简称:#abbreviationResult"(){
        given: "返回用户信息"
        userDao.getUserInfoList() >> users
        when:"获取用户信息"
        def  response= userService.getUserById(id)
        
        then: "验证返回结构是否符合预期"
        with(response) {
            postCode == postCodeResult
            abbreviation == abbreviationResult
        }
        where: "表格方式验证用户信息的分支场景"
        id | users                    || postCodeResult | abbreviationResult
        1  | getUser(1, "张三", "北京") || 100000       | "京"
        2  | getUser(2, "李四", "上海") || 200000      | "沪"
        2  | getUser(2, "李四", "上海") || 200000      | "沪"
    }

    def getUser(def id, def name, def province){
        return [new UserDTO(id: id, name: name, province: province)]
    }
}
